package com.rw.coco.reply.impl;

import com.rw.coco.api.data.model.AccountModel;
import com.rw.coco.api.reply.mapper.OptionReply;
import com.rw.coco.handler.message.event.CocoGroupMessageEvent;
import com.rw.coco.reply.constants.OptionReplyKey;
import com.rw.coco.reply.reader.CocoPropertiesReader;
import com.rw.tool.util.collection.CollectionUtil;

import java.text.MessageFormat;
import java.util.List;

public class PropertiesOptionReply implements OptionReply {
    private final CocoPropertiesReader reader;

    public PropertiesOptionReply(CocoPropertiesReader reader) {
        this.reader = reader;
    }

    @Override
    public String obIn(CocoGroupMessageEvent event) {
        String template = reader.get(OptionReplyKey.OB_IN);
        return MessageFormat.format(template, event.getNick());
    }

    @Override
    public String obExit(CocoGroupMessageEvent event) {
        String template = reader.get(OptionReplyKey.OB_EXIT);
        return MessageFormat.format(template, event.getNick());
    }

    @Override
    public String obClear(CocoGroupMessageEvent event) {
        return reader.get(OptionReplyKey.OB_CLEAR);
    }

    @Override
    public String obOn(CocoGroupMessageEvent event) {
        return reader.get(OptionReplyKey.OB_ON);
    }

    @Override
    public String obOff(CocoGroupMessageEvent event) {
        return reader.get(OptionReplyKey.OB_OFF);
    }

    @Override
    public String obList(CocoGroupMessageEvent event, List<AccountModel> groupObList) {
        if (CollectionUtil.isEmpty(groupObList)) {
            return reader.get(OptionReplyKey.OB_LIST_EMPTY);
        }

        String head = reader.get(OptionReplyKey.OB_LIST_HEAD);
        String loopTemplate = reader.get(OptionReplyKey.OB_LIST_LOOP);

        // 拼接回复字符串
        StringBuilder result = new StringBuilder(head);
        for (int i = 0; i < groupObList.size(); i++) {
            AccountModel account = groupObList.get(i);
            String format = MessageFormat.format(loopTemplate, i + 1, account.getRealName());
            result.append("\n");
            result.append(format);
        }

        return result.toString();
    }

    @Override
    public String obUnknown(CocoGroupMessageEvent event, String message) {
        String template = reader.get(OptionReplyKey.OB_UNKNOWN);
        return MessageFormat.format(template, message);
    }

    @Override
    public String obClearWithoutAuthorize(CocoGroupMessageEvent event) {
        return reader.get(OptionReplyKey.OB_CLEAR_WITHOUT_AUTHORIZE);
    }

    @Override
    public String obOnWithoutAuthorize(CocoGroupMessageEvent event) {
        return reader.get(OptionReplyKey.OB_ON_WITHOUT_AUTHORIZE);
    }

    @Override
    public String obOffWithoutAuthorize(CocoGroupMessageEvent event) {
        return reader.get(OptionReplyKey.OB_OFF_WITHOUT_AUTHORIZE);
    }

    @Override
    public String obSwitchIsNotOn(CocoGroupMessageEvent event) {
        return reader.get(OptionReplyKey.OB_SWITCH_IS_NOT_ON);
    }

    @Override
    public String obAlreadyIn(CocoGroupMessageEvent event) {
        return reader.get(OptionReplyKey.OB_ALREADY_IN);
    }
}
